package org.study.redis.api.util.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.ListDirection;
import redis.clients.jedis.args.ListPosition;
import redis.clients.jedis.params.LPosParams;
import redis.clients.jedis.util.KeyValue;

import java.util.List;

/**
 * Redis 操作工具类：List
 * 参考 {@link redis.clients.jedis.commands.ListCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisListUtils {
    private RedisListUtils() {
        throw new AssertionError("不能实例化 RedisListUtils 工具类");
    }

    public static long rpush(String key, String... elements) {
        long rpush;
        try (Jedis jedis = SysJedisPool.getResource()) {
            rpush = jedis.rpush(key, elements);
        }
        return rpush;
    }

    public static long rpush(String key, List<String> elements) {
        long rpush;
        try (Jedis jedis = SysJedisPool.getResource()) {
            rpush = jedis.rpush(key, elements.toArray(new String[elements.size()]));
        }
        return rpush;
    }

    public static long lpush(String key, String... elements) {
        long lpush;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpush = jedis.lpush(key, elements);
        }
        return lpush;
    }

    public static long lpush(String key, List<String> elements) {
        long lpush;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpush = jedis.lpush(key, elements.toArray(new String[elements.size()]));
        }
        return lpush;
    }

    public static long llen(String key) {
        long llen;
        try (Jedis jedis = SysJedisPool.getResource()) {
            llen = jedis.llen(key);
        }
        return llen;
    }

    public static List<String> lrange(String key, long start, long stop) {
        List<String> lrange = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lrange = jedis.lrange(key, start, stop);
        }
        return lrange;
    }

    public static String ltrim(String key, long start, long stop) {
        String ltrim = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            ltrim = jedis.ltrim(key, start, stop);
        }
        return ltrim;
    }

    public static String lindex(String key, long index) {
        String lindex = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lindex = jedis.lindex(key, index);
        }
        return lindex;
    }

    public static String lset(String key, long index, String value) {
        String lset = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lset = jedis.lset(key, index, value);
        }
        return lset;
    }

    public static long lrem(String key, long count, String value) {
        long lrem;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lrem = jedis.lrem(key, count, value);
        }
        return lrem;
    }

    public static String lpop(String key) {
        String lpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpop = jedis.lpop(key);
        }
        return lpop;
    }

    public static List<String> lpop(String key, int count) {
        List<String> lpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpop = jedis.lpop(key, count);
        }
        return lpop;
    }

    public static Long lpos(String key, String element) {
        Long lpos = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpos = jedis.lpos(key, element);
        }
        return lpos;
    }

    public static Long lpos(String key, String element, LPosParams params) {
        Long lpos = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpos = jedis.lpos(key, element, params);
        }
        return lpos;
    }

    public static List<Long> lpos(String key, String element, LPosParams params, long count) {
        List<Long> lpos = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpos = jedis.lpos(key, element, params, count);
        }
        return lpos;
    }

    public static String rpop(String key) {
        String rpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            rpop = jedis.rpop(key);
        }
        return rpop;
    }

    public static List<String> rpop(String key, int count) {
        List<String> rpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            rpop = jedis.rpop(key, count);
        }
        return rpop;
    }

    public static long linsert(String key, ListPosition where, String pivot, String value) {
        long linsert;
        try (Jedis jedis = SysJedisPool.getResource()) {
            linsert = jedis.linsert(key, where, pivot, value);
        }
        return linsert;
    }

    public static long lpushx(String key, String... elements) {
        long lpushx;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpushx = jedis.lpushx(key, elements);
        }
        return lpushx;
    }

    public static long lpushx(String key, List<String> elements) {
        long lpushx;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lpushx = jedis.lpushx(key, elements.toArray(new String[elements.size()]));
        }
        return lpushx;
    }

    public static long rpushx(String key, String... elements) {
        long rpushx;
        try (Jedis jedis = SysJedisPool.getResource()) {
            rpushx = jedis.rpushx(key, elements);
        }
        return rpushx;
    }

    public static long rpushx(String key, List<String> elements) {
        long rpushx;
        try (Jedis jedis = SysJedisPool.getResource()) {
            rpushx = jedis.rpushx(key, elements.toArray(new String[elements.size()]));
        }
        return rpushx;
    }

    public static List<String> blpop(int timeout, String key) {
        List<String> blpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            blpop = jedis.blpop(timeout, key);
        }
        return blpop;
    }

    public static KeyValue<String, String> blpop(double timeout, String key) {
        KeyValue<String, String> blpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            blpop = jedis.blpop(timeout, key);
        }
        return blpop;
    }

    public static List<String> brpop(int timeout, String key) {
        List<String> brpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            brpop = jedis.brpop(timeout, key);
        }
        return brpop;
    }

    public static KeyValue<String, String> brpop(double timeout, String key) {
        KeyValue<String, String> brpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            brpop = jedis.brpop(timeout, key);
        }
        return brpop;
    }

    public static List<String> blpop(int timeout, String... keys) {
        List<String> blpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            blpop = jedis.blpop(timeout, keys);
        }
        return blpop;
    }

    public static List<String> blpop(int timeout, List<String> keys) {
        List<String> blpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            blpop = jedis.blpop(timeout, keys.toArray(new String[keys.size()]));
        }
        return blpop;
    }

    public static KeyValue<String, String> blpop(double timeout, String... keys) {
        KeyValue<String, String> blpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            blpop = jedis.blpop(timeout, keys);
        }
        return blpop;
    }

    public static KeyValue<String, String> blpop(double timeout, List<String> keys) {
        KeyValue<String, String> blpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            blpop = jedis.blpop(timeout, keys.toArray(new String[keys.size()]));
        }
        return blpop;
    }

    public static List<String> brpop(int timeout, String... keys) {
        List<String> brpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            brpop = jedis.brpop(timeout, keys);
        }
        return brpop;
    }

    public static List<String> brpop(int timeout, List<String> keys) {
        List<String> brpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            brpop = jedis.brpop(timeout, keys.toArray(new String[keys.size()]));
        }
        return brpop;
    }

    public static KeyValue<String, String> brpop(double timeout, String... keys) {
        KeyValue<String, String> brpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            brpop = jedis.brpop(timeout, keys);
        }
        return brpop;
    }

    public static KeyValue<String, String> brpop(double timeout, List<String> keys) {
        KeyValue<String, String> brpop = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            brpop = jedis.brpop(timeout, keys.toArray(new String[keys.size()]));
        }
        return brpop;
    }

    public static String rpoplpush(String srckey, String dstkey) {
        String rpoplpush = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            rpoplpush = jedis.rpoplpush(srckey, dstkey);
        }
        return rpoplpush;
    }

    public static String brpoplpush(String source, String destination, int timeout) {
        String brpoplpush = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            brpoplpush = jedis.brpoplpush(source, destination, timeout);
        }
        return brpoplpush;
    }

    public static String lmove(String srckey, String dstkey, ListDirection from, ListDirection to) {
        String lmove = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            lmove = jedis.lmove(srckey, dstkey, from, to);
        }
        return lmove;
    }

    public static String blmove(String srckey, String dstkey, ListDirection from, ListDirection to, double timeout) {
        String blmove = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            blmove = jedis.blmove(srckey, dstkey, from, to, timeout);
        }
        return blmove;
    }

    /*public static void main(String[] args) {
        System.out.println("rpush：" + rpush("key:list","name", "date"));
        System.out.println("llen：" + llen("key:list"));
        System.out.println("lpush：" + lpush("key:list","name1", "date1"));
        System.out.println("llen：" + llen("key:list"));
    }*/
}
